package service

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/example/video_backend/internal/model"
	"github.com/example/video_backend/internal/repository"
)

// CommentService 评论服务接口
type CommentService interface {
	AddComment(ctx context.Context, comment *model.Comment) error
	UpdateComment(ctx context.Context, comment *model.Comment) error
	DeleteComment(ctx context.Context, commentID uint) error
	GetCommentsByVideoID(ctx context.Context, videoID uint, page, pageSize int) ([]*model.Comment, int64, error)
	GetCommentReplies(ctx context.Context, parentID uint, page, pageSize int) ([]*model.Comment, int64, error)
	LikeComment(ctx context.Context, commentID uint) error
	UnlikeComment(ctx context.Context, commentID uint) error
}

// commentService 实现CommentService接口
type commentService struct {
	commentRepo repository.CommentRepository
	videoRepo   repository.VideoRepository
}

// NewCommentService 创建评论服务实例
func NewCommentService(
	commentRepo repository.CommentRepository,
	videoRepo repository.VideoRepository,
) CommentService {
	return &commentService{
		commentRepo: commentRepo,
		videoRepo:   videoRepo,
	}
}

// AddComment 添加评论
func (s *commentService) AddComment(ctx context.Context, comment *model.Comment) error {
	// 设置创建和更新时间
	comment.CreatedAt = time.Now()
	comment.UpdatedAt = time.Now()

	// 创建评论
	if err := s.commentRepo.Create(ctx, comment); err != nil {
		return fmt.Errorf("创建评论失败: %w", err)
	}

	// 如果是一级评论（不是回复），增加视频评论数
	if comment.ParentID == nil {
		if err := s.videoRepo.IncreaseCommentsCount(ctx, comment.VideoID); err != nil {
			return fmt.Errorf("更新视频评论数失败: %w", err)
		}
	}

	return nil
}

// UpdateComment 更新评论
func (s *commentService) UpdateComment(ctx context.Context, comment *model.Comment) error {
	// 查找评论
	existingComment, err := s.commentRepo.GetByID(ctx, comment.ID)
	if err != nil {
		return err
	}
	if existingComment == nil {
		return errors.New("评论不存在")
	}

	// 更新评论内容
	existingComment.Content = comment.Content
	existingComment.UpdatedAt = time.Now()

	return s.commentRepo.Update(ctx, existingComment)
}

// DeleteComment 删除评论
func (s *commentService) DeleteComment(ctx context.Context, commentID uint) error {
	// 查找评论
	comment, err := s.commentRepo.GetByID(ctx, commentID)
	if err != nil {
		return err
	}
	if comment == nil {
		return errors.New("评论不存在")
	}

	// 删除评论
	if err := s.commentRepo.Delete(ctx, commentID); err != nil {
		return fmt.Errorf("删除评论失败: %w", err)
	}

	// 如果是一级评论（不是回复），减少视频评论数
	if comment.ParentID == nil {
		if err := s.videoRepo.DecreaseCommentsCount(ctx, comment.VideoID); err != nil {
			return fmt.Errorf("更新视频评论数失败: %w", err)
		}
	}

	return nil
}

// GetCommentsByVideoID 获取视频的评论列表
func (s *commentService) GetCommentsByVideoID(ctx context.Context, videoID uint, page, pageSize int) ([]*model.Comment, int64, error) {
	return s.commentRepo.ListByVideoID(ctx, videoID, page, pageSize)
}

// GetCommentReplies 获取评论的回复
func (s *commentService) GetCommentReplies(ctx context.Context, parentID uint, page, pageSize int) ([]*model.Comment, int64, error) {
	return s.commentRepo.ListReplies(ctx, parentID, page, pageSize)
}

// LikeComment 点赞评论
func (s *commentService) LikeComment(ctx context.Context, commentID uint) error {
	return s.commentRepo.IncreaseLikesCount(ctx, commentID)
}

// UnlikeComment 取消点赞评论
func (s *commentService) UnlikeComment(ctx context.Context, commentID uint) error {
	return s.commentRepo.DecreaseLikesCount(ctx, commentID)
}